/**
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.smallchill.core.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

/**
 * @title Jackson工具类
 * @author zhuangqian
 * @email smallchill@163.com
 * @date 2016-12-3下午11:48:49
 * @copyright 2016
 */
public class JacksonKit {

    private static ObjectMapper objectMapper;

    private static XmlMapper xmlMapper;
    
    static {
    	objectMapper = new ObjectMapper();
    	xmlMapper = new XmlMapper();
    }
    
    public static <T> T xmlToBean(String content, Class<T> cls) {
        try {
			return xmlMapper.readValue(content, cls);
		} catch (IOException e) {
			return null;
		}
    }

    public static <T> T xmlToBean(File xmlFile, Class<T> cls){
        try {
			return xmlMapper.readValue(xmlFile, cls);
		} catch (IOException e) {
			return null;
		}
    }

    public static <T> T xmlToBean(InputStream xmlInputStream, Class<T> cls) {
        try {
			return xmlMapper.readValue(xmlInputStream, cls);
		} catch (IOException e) {
			return null;
		}
    }

    public static <T> String beanToXml(T bean) {
        try {
			return xmlMapper.writeValueAsString(bean);
		} catch (JsonProcessingException e) {
			return null;
		}
    }
    
    public static <T> T jsonToBean(String content, Class<T> cls)  {
        try {
			return objectMapper.readValue(content, cls);
		} catch (IOException e) {
			return null;
		}
    }
    
    public static <T> T jsonToBean(InputStream jsonInputStream, Class<T> cls)  {
        try {
			return objectMapper.readValue(jsonInputStream, cls);
		} catch (IOException e) {
			return null;
		}
    }
    
    public static <T> String beanToJson(T bean) {
    	 try {
			return objectMapper.writeValueAsString(bean);
		} catch (JsonProcessingException e) {
			return null;
		} 
    } 
    
}
